package com.ruoyi.basebill.util;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.ruoyi.basebill.domain.*;
import com.ruoyi.basebill.service.*;
import com.ruoyi.basebill.task.util.OmsTaskUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysConfigService;
import io.jsonwebtoken.lang.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Component
public class SendEMail {
    @Autowired
    private JavaMailSender mailSender;

    //发货确认服务层接口
    @Autowired
    private IOmsDeliverbillService deliverbillService;
    //收货确认服务层接口
    @Autowired
    private IOmsReceiptbillService receiptbillService;
    //销售退货服务层接口
    @Autowired
    private IOmsSalereturnbillService salereturnbillService;
    //调拨单服务层接口
    @Autowired
    private IOmsTransferbillService transferbillService;
    //销出单服务层接口
    @Autowired
    private IOmsSaleissuebillService saleissuebillService;
    //其他出库单服务层接口
    @Autowired
    private IOmsOtherissuebillService otherissuebillService;
    //汇总销出单服务层接口
    @Autowired
    private IOmsSumsaleissuebillService sumsaleissuebillService;
    //汇总采入单服务层接口
    @Autowired
    private IOmsSumpurinwarehsbillService sumpurinwarehsbillService;
    //汇总库存调拨单服务层接口
    @Autowired
    private IOmsSumstocktransferbillService sumstocktransferbillService;
    //系统参数服务层接口
    @Autowired
    private ISysConfigService sysConfigService;

    //工具类
    @Autowired
    private OmsTaskUtil omsTaskUtil;


    @Value("${spring.mail.username}")
    private String from;

    public static final String spanBefore = "<span style='font-size:18px;color:red;'>";
    public static final String spanAfter = "</span>";

    /**
     * 发送邮件
     */
    public void sendSimpleMail(){
        try {
            MimeMessage mimeMessage = mailSender.createMimeMessage();
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
            StringBuffer stringBuffer = new StringBuffer();
            Boolean isErr = false;
            selectDeliverBill(stringBuffer,isErr);
            selectReceBill(stringBuffer,isErr);
            selectSaleReturn(stringBuffer,isErr);
            selectTransfer(stringBuffer,isErr);
            selectSaleBill(stringBuffer,isErr);
            selectOther(stringBuffer,isErr);
            selectSumSale(stringBuffer,isErr);
            selectSumPur(stringBuffer,isErr);
            selectSumStoc(stringBuffer,isErr);
            selectRepeatBill(stringBuffer,isErr);
            if (stringBuffer.length() > 0){
                stringBuffer.insert(0,"<!DOCTYPE html><html><body>");
                stringBuffer.append("<p>点击前往<a href='http://125.124.200.64:82/'>结算中心</a></p>");
                stringBuffer.append("</body></html>");
                mimeMessageHelper.setSubject("结算中心问题报告");
                mimeMessageHelper.setText(stringBuffer.toString(),true);
                String qmConfig = sysConfigService.selectConfigByKey("toEmail");
//                String to = "{'toEmail':['2957214543@qq.com','2403326863@qq.com']}";
                JSONObject jsonObject = JSON.parseObject(qmConfig);
                ArrayList<String> toEmail = (ArrayList<String>) jsonObject.get("toEmail");
                String[] strings = toEmail.toArray(new String[toEmail.size()]);
                mimeMessageHelper.setTo(strings);
                mimeMessageHelper.setFrom(from);
                mailSender.send(mimeMessage);
            }
        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }


    }

    /**
     * 发货确认查询异常
     */
    public void selectDeliverBill(StringBuffer stringBuffer,Boolean isErr){
        OmsDeliverbill omsDeliverbill = new OmsDeliverbill();
        omsDeliverbill.setIscreatebill(-1L);
        List<OmsDeliverbill> omsDeliverbills = deliverbillService.selectOmsDeliverbillList(omsDeliverbill);
        if (StringUtils.isNotEmpty(omsDeliverbills)){
            stringBuffer.append("<p>");
            stringBuffer.append("发货确认内部关联单据下发失败单据有：");
            stringBuffer.append(spanBefore);
            stringBuffer.append(omsDeliverbills.size());
            stringBuffer.append(spanAfter);
            stringBuffer.append("</p>");
            isErr = true;
        }
        omsDeliverbill.setIscreatebill(-3L);
        List<OmsDeliverbill> omsDeliverbills2 = deliverbillService.selectOmsDeliverbillList(omsDeliverbill);
        if (StringUtils.isNotEmpty(omsDeliverbills2)){
            stringBuffer.append("<p>");
            stringBuffer.append("发货确认内部关联单据异常单据有：");
            stringBuffer.append(spanBefore);
            stringBuffer.append(omsDeliverbills2.size());
            stringBuffer.append(spanAfter);
            stringBuffer.append("</p>");
            isErr = true;
        }
    }

    /**
     * 收货确认查询异常
     * @param stringBuffer
     * @param isErr
     */
    public void selectReceBill(StringBuffer stringBuffer,Boolean isErr){
        //查询内部关联单据是否存在问题
        OmsReceiptbill inBill = new OmsReceiptbill();
        inBill.setIscreatebill(-1L);
        List<OmsReceiptbill> omsReceiptbills = receiptbillService.selectOmsReceiptbillList(inBill);
        if (StringUtils.isNotEmpty(omsReceiptbills)){
            stringBuffer.append("<p>");
            stringBuffer.append("收货确认内部关联单据下发失败单据有：");
            stringBuffer.append(spanBefore);
            stringBuffer.append(omsReceiptbills.size());
            stringBuffer.append(spanAfter);
            stringBuffer.append("</p>");
            isErr = true;
        }
        inBill.setIscreatebill(-3L);
        List<OmsReceiptbill> omsReceiptbills2 = receiptbillService.selectOmsReceiptbillList(inBill);
        if (StringUtils.isNotEmpty(omsReceiptbills2)){
            stringBuffer.append("<p>");
            stringBuffer.append("收货确认内部关联单据异常单据有：");
            stringBuffer.append(spanBefore);
            stringBuffer.append(omsReceiptbills2.size());
            stringBuffer.append(spanAfter);
            stringBuffer.append("</p>");
            isErr = true;
        }
        //对外下发状态
        OmsReceiptbill outBill = new OmsReceiptbill();
        outBill.setIscreateoutbill(-1L);
        List<OmsReceiptbill> omsReceiptbills3 = receiptbillService.selectOmsReceiptbillList(outBill);
        if (StringUtils.isNotEmpty(omsReceiptbills3)){
            stringBuffer.append("<p>");
            stringBuffer.append("收货确认对外下发失败单据有：");
            stringBuffer.append(spanBefore);
            stringBuffer.append(omsReceiptbills3.size());
            stringBuffer.append(spanAfter);
            stringBuffer.append("</p>");
            isErr = true;
        }

    }

    /**
     * 销售退货查询异常
     */
    public void selectSaleReturn(StringBuffer stringBuffer,Boolean isErr){
        //查询内部关联单据是否存在问题
        OmsSalereturnbill inBill = new OmsSalereturnbill();
        inBill.setIscreatebill(-1L);
        List<OmsSalereturnbill> omsSalereturnbills = salereturnbillService.selectOmsSalereturnbillList(inBill);
        if (StringUtils.isNotEmpty(omsSalereturnbills)){
            stringBuffer.append("<p>");
            stringBuffer.append("销售退货内部关联单据下发失败单据有：");
            stringBuffer.append(spanBefore);
            stringBuffer.append(omsSalereturnbills.size());
            stringBuffer.append(spanAfter);
            stringBuffer.append("</p>");
            isErr = true;
        }
        inBill.setIscreatebill(-3L);
        List<OmsSalereturnbill> omsSalereturnbills2 = salereturnbillService.selectOmsSalereturnbillList(inBill);
        if (StringUtils.isNotEmpty(omsSalereturnbills2)){
            stringBuffer.append("<p>");
            stringBuffer.append("销售退货内部关联单据异常单据有：");
            stringBuffer.append(spanBefore);
            stringBuffer.append(omsSalereturnbills2.size());
            stringBuffer.append(spanAfter);
            stringBuffer.append("</p>");
            isErr = true;
        }
        //对外下发状态
        OmsSalereturnbill outBill = new OmsSalereturnbill();
        outBill.setIscreateoutbill(-1L);
        List<OmsSalereturnbill> omsSalereturnbills3 = salereturnbillService.selectOmsSalereturnbillList(outBill);
        if (StringUtils.isNotEmpty(omsSalereturnbills3)){
            stringBuffer.append("<p>");
            stringBuffer.append("销售退货对外下发失败单据有：");
            stringBuffer.append(spanBefore);
            stringBuffer.append(omsSalereturnbills3.size());
            stringBuffer.append(spanAfter);
            stringBuffer.append("</p>");
            isErr = true;
        }
    }

    /**
     * 调拨单查询异常
     * @param stringBuffer
     * @param isErr
     */
    public void selectTransfer(StringBuffer stringBuffer,Boolean isErr){
        OmsTransferbill omsTransferbill = new OmsTransferbill();
        omsTransferbill.setIscreatebill(-1L);
        List<OmsTransferbill> omsTransferbills = transferbillService.selectOmsTransferbillList(omsTransferbill);
        if (StringUtils.isNotEmpty(omsTransferbills)){
            stringBuffer.append("<p>");
            stringBuffer.append("调拨单内部关联单据下发失败单据有：");
            stringBuffer.append(spanBefore);
            stringBuffer.append(omsTransferbills.size());
            stringBuffer.append(spanAfter);
            stringBuffer.append("</p>");
            isErr = true;
        }
        omsTransferbill.setIscreatebill(-3L);
        List<OmsTransferbill> omsTransferbills2 = transferbillService.selectOmsTransferbillList(omsTransferbill);
        if (StringUtils.isNotEmpty(omsTransferbills2)){
            stringBuffer.append("<p>");
            stringBuffer.append("调拨单内部关联单据异常单据有：");
            stringBuffer.append(spanBefore);
            stringBuffer.append(omsTransferbills2.size());
            stringBuffer.append(spanAfter);
            stringBuffer.append("</p>");
            isErr = true;
        }
    }

    /**
     * 销出单查询异常
     */
    public void selectSaleBill(StringBuffer stringBuffer,Boolean isErr){
        OmsSaleissuebill omsSaleissuebill = new OmsSaleissuebill();
        omsSaleissuebill.setIsoutcustomer(1L);
        omsSaleissuebill.setIstrans("0");
        omsSaleissuebill.setReason("推送金蝶失败");
        List<OmsSaleissuebill> omsSaleissuebills = saleissuebillService.selectOmsSaleissuebillList(omsSaleissuebill);
        if (StringUtils.isNotEmpty(omsSaleissuebills)){
            stringBuffer.append("<p>");
            stringBuffer.append("销出单推送EAS失败单据有：");
            stringBuffer.append(spanBefore);
            stringBuffer.append(omsSaleissuebills.size());
            stringBuffer.append(spanAfter);
            stringBuffer.append("</p>");
            isErr = true;
        }
    }

    /**
     * 其他出库单查询异常
     */
    public void selectOther(StringBuffer stringBuffer,Boolean isErr){
        OmsOtherissuebill omsOtherissuebill = new OmsOtherissuebill();
        omsOtherissuebill.setIstrans("0");
        omsOtherissuebill.setReason("推送金蝶失败");
        List<OmsOtherissuebill> omsOtherissuebills = otherissuebillService.selectOmsOtherissuebillList(omsOtherissuebill);
        if (StringUtils.isNotEmpty(omsOtherissuebills)){
            stringBuffer.append("<p>");
            stringBuffer.append("其他出库单推送EAS失败单据有：");
            stringBuffer.append(spanBefore);
            stringBuffer.append(omsOtherissuebills.size());
            stringBuffer.append(spanAfter);
            stringBuffer.append("</p>");
            isErr = true;
        }
    }

    /**
     * 汇总销出单查询异常
     */
    public void selectSumSale(StringBuffer stringBuffer,Boolean isErr){
        OmsSumsaleissuebill omsSumsaleissuebill = new OmsSumsaleissuebill();
        omsSumsaleissuebill.setIstrans("0");
        omsSumsaleissuebill.setReason("推送金蝶失败");
        List<OmsSumsaleissuebill> omsSumsaleissuebills = sumsaleissuebillService.selectOmsSumsaleissuebillList(omsSumsaleissuebill);
        if (StringUtils.isNotEmpty(omsSumsaleissuebills)){
            stringBuffer.append("<p>");
            stringBuffer.append("汇总销出单推送EAS失败单据有：");
            stringBuffer.append(spanBefore);
            stringBuffer.append(omsSumsaleissuebills.size());
            stringBuffer.append(spanAfter);
            stringBuffer.append("</p>");
            isErr = true;
        }
    }

    /**
     * 汇总采入单查询异常
     */
    public void selectSumPur(StringBuffer stringBuffer,Boolean isErr){
        OmsSumpurinwarehsbill omsSumpurinwarehsbill = new OmsSumpurinwarehsbill();
        omsSumpurinwarehsbill.setIstrans("0");
        omsSumpurinwarehsbill.setReason("推送金蝶失败");
        List<OmsSumpurinwarehsbill> omsSumpurinwarehsbills = sumpurinwarehsbillService.selectOmsSumpurinwarehsbillList(omsSumpurinwarehsbill);
        if (StringUtils.isNotEmpty(omsSumpurinwarehsbills)){
            stringBuffer.append("<p>");
            stringBuffer.append("汇总采入单推送EAS失败单据有：");
            stringBuffer.append(spanBefore);
            stringBuffer.append(omsSumpurinwarehsbills.size());
            stringBuffer.append(spanAfter);
            stringBuffer.append("</p>");
            isErr = true;
        }
    }

    /**
     * 汇总库存调拨单查询异常
     */
    public void selectSumStoc(StringBuffer stringBuffer,Boolean isErr){
        OmsSumstocktransferbill omsSumstocktransferbill = new OmsSumstocktransferbill();
        omsSumstocktransferbill.setIstrans("0");
        omsSumstocktransferbill.setReason("推送金蝶失败");
        List<OmsSumstocktransferbill> omsSumstocktransferbills = sumstocktransferbillService.selectOmsSumstocktransferbillList(omsSumstocktransferbill);
        if (StringUtils.isNotEmpty(omsSumstocktransferbills)){
            stringBuffer.append("<p>");
            stringBuffer.append("汇总库存调拨单推送EAS失败单据有：");
            stringBuffer.append(spanBefore);
            stringBuffer.append(omsSumstocktransferbills.size());
            stringBuffer.append(spanAfter);
            stringBuffer.append("</p>");
            isErr = true;
        }
    }

    public void selectRepeatBill(StringBuffer stringBuffer,Boolean isErr){
        String rece = "t_oms_receiptbill";
        String delive = "t_oms_deliverbill";
        String salereturn = "t_oms_salereturnbill";
        String trans = "t_oms_transferbill";
        Date nowMonthDate = omsTaskUtil.getNowMonthDate();
        //获取收货确认
        List<String> strings = omsTaskUtil.selectRepeatBill(rece, nowMonthDate);
        if (null != strings && 0 != strings.size()){
            stringBuffer.append("<p>");
            stringBuffer.append("收货确认单据中重复的有：");
            stringBuffer.append(spanBefore);
            stringBuffer.append(Arrays.toString(strings.toArray()));
            stringBuffer.append(spanAfter);
            stringBuffer.append("</p>");
        }
        //获取发货确认
        List<String> strings2 = omsTaskUtil.selectRepeatBill(delive, nowMonthDate);
        if (null != strings2 && 0 != strings2.size()){
            stringBuffer.append("<p>");
            stringBuffer.append("发货确认单据中重复的有：");
            stringBuffer.append(spanBefore);
            stringBuffer.append(Arrays.toString(strings2.toArray()));
            stringBuffer.append(spanAfter);
            stringBuffer.append("</p>");
        }
        //获取销售退货
        List<String> strings3 = omsTaskUtil.selectRepeatBill(salereturn, nowMonthDate);
        if (null != strings3 && 0 != strings3.size()){
            stringBuffer.append("<p>");
            stringBuffer.append("销售退货单据中重复的有：");
            stringBuffer.append(spanBefore);
            stringBuffer.append(Arrays.toString(strings3.toArray()));
            stringBuffer.append(spanAfter);
            stringBuffer.append("</p>");
        }
        //获取调拨单
        List<String> strings4 = omsTaskUtil.selectRepeatBill(trans, nowMonthDate);
        if (null != strings4 && 0 != strings4.size()){
            stringBuffer.append("<p>");
            stringBuffer.append("调拨单单据中重复的有：");
            stringBuffer.append(spanBefore);
            stringBuffer.append(Arrays.toString(strings4.toArray()));
            stringBuffer.append(spanAfter);
            stringBuffer.append("</p>");
        }
    }



}
